package com.bienvenue.main.ui.activity.enterprise;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.bienvenue.main.R;
import com.bienvenue.main.base.BaseActivity;
import com.bienvenue.main.httpService.ResponseObserver;
import com.bienvenue.main.model.HttpBean;
import com.bienvenue.main.serviceApi.ServiceModel;
import com.bienvenue.main.ui.activity.enterprise.BusinessLicenseCertificationActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ProjectName: Bienvenue
 * @Package: com.bienvenue.main.ui.activity
 * @ClassName: EnterpriseRegisterActivity
 * @Description: 企业注册
 * @Author: Mr.Chen
 * @CreateDate: 2020/9/9 16:03
 * @UpdateUser: Mr.Chen
 * @UpdateDate: 2020/9/9 16:03
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */


public class EnterpriseRegisterActivity extends BaseActivity {
    @BindView(R.id.et_personal_phone)
    EditText etPersonalPhone;
    @BindView(R.id.et_enterprise_name)
    EditText etEnterPriseName;
    @BindView(R.id.et_input_pwd)
    EditText etInputPwd;
    @BindView(R.id.et_input_enter_pwd)
    EditText etInputEnterPwd;
    @BindView(R.id.et_send_sms_code_label)
    EditText etSendSmsCodeLabel;
    @BindView(R.id.tv_send_sms_code)
    TextView tvSendSmsCode;

    MaterialDialog dialog;

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        dialog=initDialog("请稍后...");

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_enterprise_register;
    }

    @OnClick(R.id.iv_return_btn)
    void onFinish() {
        this.finish();
    }

    @OnClick(R.id.tv_send_sms_code)
    void onSendSmsCode() {
        if (TextUtils.isEmpty(etPersonalPhone.getText().toString().trim()) || etPersonalPhone.getText().toString().trim().length() <= 10) {
            showToast("请输入正确的手机号");
        } else {
            smsCode();
        }
    }

    public void smsCode() {
        CountDownTimer mTimer = new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvSendSmsCode.setTextColor(Color.GRAY);
                tvSendSmsCode.setText(millisUntilFinished / 1000 + "秒后重发");
            }

            @Override
            public void onFinish() {
                tvSendSmsCode.setEnabled(true);
                tvSendSmsCode.setTextColor(getColor(R.color.color_478CFE));
                tvSendSmsCode.setText("获取验证码");
                cancel();
            }
        };
        mTimer.start();
        tvSendSmsCode.setEnabled(false);
        ServiceModel.getServiceModel().onSuperCode(etPersonalPhone.getText().toString().trim(), new ResponseObserver<HttpBean>() {
            @Override
            public void onSuccess(HttpBean response) {
                if (response.getCode() == 200) {
                    showToast("验证码已下发，请注意查收");


                } else {
                    showToast(response.getMsg());
                    tvSendSmsCode.setEnabled(false);

                }
            }

            @Override
            public void onFail(String message) {
                showToast(message);
                tvSendSmsCode.setEnabled(false);
            }
        });
    }

    @OnClick(R.id.tv_next_btn)
    void onNext() {
        if (TextUtils.isEmpty(etEnterPriseName.getText().toString().trim()) || etEnterPriseName.getText().length() == 0) {
            showToast("请填写企业名称");
            return;
        } else if (TextUtils.isEmpty(etPersonalPhone.getText().toString().trim()) || (etPersonalPhone.getText().toString().trim().length() <= 10 && etPersonalPhone.getText().toString().length() >= 0)) {
            showToast("请填写正确的手机号");
            return;
        } else if (TextUtils.isEmpty(etInputPwd.getText().toString().trim()) || etInputEnterPwd.getText().toString().trim().length() == 0) {
            showToast("请输入正确格式的密码");
            return;
        } else if (TextUtils.isEmpty(etInputEnterPwd.getText().toString().trim()) || !etInputEnterPwd.getText().toString().trim().equals(etInputPwd.getText().toString().trim())) {
            showToast("两次密码输入不一致");
            return;
        } else if (TextUtils.isEmpty(etSendSmsCodeLabel.getText().toString().trim()) || etSendSmsCodeLabel.getText().length() == 0) {
            showToast("请填写验证码");
            return;
        } else {

            dialog.show();
            ServiceModel.getServiceModel().onVaildCompanyName(etEnterPriseName.getText().toString().trim(), new ResponseObserver<HttpBean>() {
                @Override
                public void onSuccess(HttpBean response) {
                    dialog.dismiss();
                    if (response.getCode() == 200) {
                        Intent intent=new Intent(EnterpriseRegisterActivity.this, BusinessLicenseCertificationActivity.class);
                        Bundle bundle=new Bundle();
                        bundle.putString("enterpriseName",etEnterPriseName.getText().toString().trim());
                        bundle.putString("phone",etPersonalPhone.getText().toString().trim());
                        bundle.putString("pwd",etInputEnterPwd.getText().toString().trim());
                        bundle.putString("superCode",etSendSmsCodeLabel.getText().toString().trim());
                        intent.putExtras(bundle);
                        startActivity(intent);
                    } else {
                        showToast(response.getMsg());
                    }
                }

                @Override
                public void onFail(String message) {
                    Log.e("TAG", message);
                    dialog.dismiss();
                }
            });
        }

    }

}
